package com.daydayup.learn;


import cn.hutool.json.JSONUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class SpringControllerAdvice {

    @InitBinder
    public void initBinder(WebDataBinder binder){
        System.out.println("initBinder:"+JSONUtil.toJsonStr(binder));
    }

    @ModelAttribute
    public void addAttributes(Model model) {
        model.addAttribute("words","hello world");
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Map errorHandler(Exception ex) {
        Map map = new HashMap();
        map.put("code",100);
        map.put("msg",ex.getMessage());
        return map;
    }

}
